package com.rahms.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rahms.pojo.entity.Notification;
import com.rahms.server.mapper.NotificationMapper;
import com.rahms.server.service.NotificationService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 消息通知服务实现类
 */
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements NotificationService {
    
    @Override
    public List<Notification> getNotificationsByUserId(Long userId, String type, Integer isRead) {
        LambdaQueryWrapper<Notification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notification::getUserId, userId)
                   .eq(type != null && !type.isEmpty(), Notification::getNotiType, type)
                   .eq(isRead != null, Notification::getIsRead, isRead)
                   .orderByDesc(Notification::getCreateTime);
        return this.list(queryWrapper);
    }
    
    @Override
    public int getUnreadCountByUserId(Long userId) {
        LambdaQueryWrapper<Notification> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Notification::getUserId, userId)
                   .eq(Notification::getIsRead, 0);
        return (int) this.count(queryWrapper);
    }
    
    @Override
    public boolean markAsRead(Long notiId) {
        Notification notification = this.getById(notiId);
        if (notification != null) {
            notification.setIsRead(1);
            return this.updateById(notification);
        }
        return false;
    }
    
    @Override
    public boolean batchMarkAsRead(List<Long> notiIds) {
        if (notiIds == null || notiIds.isEmpty()) {
            return true;
        }
        LambdaQueryWrapper<Notification> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.in(Notification::getNotiId, notiIds);
        Notification notification = new Notification();
        notification.setIsRead(1);
        return this.update(notification, updateWrapper);
    }
}